package com.web.spider.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @Author cherrish
 * @Date 2019/8/27 10:31
 * @Version: 1.0
 * @Description
 */
public class HttpUtils {
	private final static String CHARSET_UTF_8 = "UTF-8";
	private final static String CONTENT_TYPE = "Content-Type";
	private final static String APPLICATION_JSON = "application/json";
	private final static String APPLICATION_FORM = "application/x-www-form-urlencoded; charset=utf-8";

	public static String post(String url, Map<String, String> header, String jsonStr){
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPost httpPost = new HttpPost(url);
		setRequestConfig(httpPost);
		if(null == header || header.size() < 1){
			httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON);
		}else {
			setHeader(httpPost, header);
		}
		StringEntity requestEntity = new StringEntity(jsonStr, CHARSET_UTF_8);
		//设置请求体
		httpPost.setEntity(requestEntity);

		CloseableHttpResponse httpResponse = null;
		try {
			httpResponse = httpClient.execute(httpPost, new BasicHttpContext());
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if(HttpStatus.SC_OK != statusCode){
				return null;
			}
			return EntityUtils.toString(httpResponse.getEntity(), CHARSET_UTF_8);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			// 关闭
		}
		return null;
	}
	public static String get(String url, Map<String, String> header, Map<String, Object> params){
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		setRequestConfig(httpGet);
		setHeader(httpGet, header);
		CloseableHttpResponse httpResponse = null;
		try {
			httpResponse = httpClient.execute(httpGet);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if(HttpStatus.SC_OK != statusCode){
				return null;
			}
			return EntityUtils.toString(httpResponse.getEntity(), CHARSET_UTF_8);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			// 关闭
		}
		return null;
	}

	private static void setHeader(HttpRequestBase httpRequestBase, Map<String, String> header){
		if(null != httpRequestBase && null != header && header.size() > 0){
			for(Map.Entry<String, String> entry : header.entrySet()){
				httpRequestBase.addHeader(entry.getKey(), entry.getValue());
			}
		}
	}

	private static void resetHeaderOfResp(HttpResponse httpResponse, Map<String, String> headerMap){
		if(null != httpResponse && null != headerMap){
			Header[] headers = httpResponse.getAllHeaders();
			for(Header header : headers){
				headerMap.put(header.getName(), header.getValue());
			}
		}
	}

	private static void setRequestConfig(HttpRequestBase httpRequestBase){
		if(null != httpRequestBase){
			//设置请求的状态参数 单位毫秒
			RequestConfig requestConfig = RequestConfig.custom()
					.setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(5000).build();
			httpRequestBase.setConfig(requestConfig);
		}
	}
}
